package com.jiazhong.test;

import com.jiazhong.testtwo.mapper.BrandMapper;
import com.jiazhong.testtwo.pojo.Brand;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class test01 {
    public static void main(String[] args) throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2. 获取SqlSession对象，用它来执行sql
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //3. 执行sql
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        List<Brand> brands = brandMapper.selectAll();
        System.out.println(brands);
        //4. 释放资源
        sqlSession.close();
    }

    @Test
    public void selectById() throws IOException {
        int id = 1;
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2. 获取SqlSession对象，用它来执行sql
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //3. 执行sql
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
//        List<Brand> brands = brandMapper.selectAll();
        Brand brands = brandMapper.selectById(1);
        System.out.println(brands);
        //4. 释放资源
        sqlSession.close();
    }

    @Test
    public void selectByCondition() throws IOException {
        Integer status = 1;
        String brandName = "小米";
        String companyName = "小米";

        brandName = "%" + brandName + "%";
        companyName = "%" + companyName + "%";

        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2. 获取SqlSession对象，用它来执行sql
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //3. 执行sql
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        //第一种
//     List<Brand> brands=brandMapper.selectByCondition(status,brandName,companyName);
        //第二种
//        Brand brand = new Brand();
//        brand.setStatus(status);
//        brand.setBrandName(brandName);
//        brand.setCompanyName(companyName);
//        List<Brand> brands = brandMapper.selectByCondition(brand);
        Map map = new HashMap<>();
        map.put("status", status);
        map.put("brandName", brandName);
        map.put("companyName", companyName);
        List<Brand> brands = brandMapper.selectByCondition(map);
        System.out.println(brands);

        //4. 释放资源
        sqlSession.close();
    }

    @Test
    //动态sql使用
    public void a() throws IOException {
        Integer status = 1;
        String brandName = "小米";
        String companyName = "小米";

        brandName = "%" + brandName + "%";
        companyName = "%" + companyName + "%";

        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2. 获取SqlSession对象，用它来执行sql
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //3. 执行sql
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);

        Brand brand = new Brand();
        brand.setStatus(status);
//        brand.setBrandName(brandName);
        brand.setCompanyName(companyName);
        List<Brand> brands = brandMapper.dongtaisql(brand);
        System.out.println(brands);
        //4. 释放资源
        sqlSession.close();
    }

    @Test
    //单条件查询
    public void b() throws IOException {
        Integer status = 1;
        String brandName = "小米";
        String companyName = "小米";

        brandName = "%" + brandName + "%";
        companyName = "%" + companyName + "%";

        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2. 获取SqlSession对象，用它来执行sql
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //3. 执行sql
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);

        Brand brand = new Brand();
        brand.setStatus(status);
//        brand.setBrandName(brandName);
        brand.setCompanyName(companyName);
        List<Brand> brands = brandMapper.selectByConditionSingle(brand);
        System.out.println(brands);
        //4. 释放资源
        sqlSession.close();
    }

    @Test
    //插入语句
    public void c() throws IOException {
        Integer status = 2;
        String brandName = "哇哈哈";
        String companyName = "哇哈哈有限公司";
        int ordered = 400;
        String description = "好喝";
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2. 获取SqlSession对象，用它来执行sql,也可提交事务，openSession()默认不提交，所以如果openSession无参，那么就要在下m面写sqlSession.commit();
//        SqlSession sqlSession = sqlSessionFactory.openSession();
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        //3. 执行sql
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);

        Brand brand = new Brand();
        brand.setStatus(status);
        brand.setBrandName(brandName);
        brand.setCompanyName(companyName);
        brand.setOrdered(ordered);
        brand.setDescription(description);
        brandMapper.add(brand);
        System.out.println(brand.getId());
        //事务的提交
//        sqlSession.commit();
        //4. 释放资源
        sqlSession.close();
    }

    @Test
    //更新语句
    public void update() throws IOException {
        Integer status = 0;
        String brandName = "哇哈哈";
        String companyName = "哇哈哈有限公司";
        int ordered = 400;
        int id = 7;
        String description = "好喝";
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2. 获取SqlSession对象，用它来执行sql,也可提交事务，openSession()默认不提交，所以如果openSession无参，那么就要在下m面写sqlSession.commit();
//        SqlSession sqlSession = sqlSessionFactory.openSession();
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        //3. 执行sql
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);

        Brand brand = new Brand();
        brand.setStatus(status);
        brand.setBrandName(brandName);
        brand.setCompanyName(companyName);
        brand.setOrdered(ordered);
        brand.setDescription(description);
        brand.setId(id);
        brandMapper.update(brand);
        System.out.println("ok");
        //事务的提交
//        sqlSession.commit();
        //4. 释放资源
        sqlSession.close();
    }
    @Test
    //单个删除语句
    public void delecte() throws IOException {
        int id = 8;
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2. 获取SqlSession对象，用它来执行sql,也可提交事务，openSession()默认不提交，所以如果openSession无参，那么就要在下m面写sqlSession.commit();
//        SqlSession sqlSession = sqlSessionFactory.openSession();
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        //3. 执行sql
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        Brand brand = new Brand();
        brand.setId(id);
        brandMapper.delete(id);
        System.out.println("ok");
        //事务的提交
//        sqlSession.commit();
        //4. 释放资源
        sqlSession.close();
    }

    @Test
    //批量删除
    public void delectes() throws IOException {
        int []ids = {5,6,7};
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2. 获取SqlSession对象，用它来执行sql,也可提交事务，openSession()默认不提交，所以如果openSession无参，那么就要在下m面写sqlSession.commit();
//        SqlSession sqlSession = sqlSessionFactory.openSession();
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        //3. 执行sql
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        brandMapper.deletes(ids);
        System.out.println("ok");
        //事务的提交
//        sqlSession.commit();
        //4. 释放资源
        sqlSession.close();
    }
}